package org.dei.perla.utils.testframework;

import java.util.*;


public class LineParser {

	/**
	 * Analizza la linea e la suddivide in blocchi.
	 * 
	 * @param parLine Linea che deve essere suddivisa
	 * @return Array contenente la suddivisione
	 */
	public String[] parse(String parLine) {
		
		char[] tArrayString = parLine.toCharArray();
		ArrayList<String> tStringArrayList = new ArrayList<String>();
		String tString = new String("");
		int tCursor=0;
		boolean tFlag=false;
		
		while (tCursor!=parLine.length()) {
			
		if (tArrayString[tCursor]=='"') {
				
				int k=tCursor+1;
				while (tArrayString[k]!='"') {
					
					tString=tString+parLine.charAt(k);
					k++;					
				}
				tFlag=false;
				tCursor=k+1;
			}
			
		else if (tArrayString[tCursor]==' ' || tArrayString[tCursor]=='\t') {
			
			if (tFlag==true) {/*spazi ripetuti, skippa*/tCursor++;}
			else {
				
				tStringArrayList.add(tString);
				tFlag=true;
				tCursor++;
				tString = "";				
			}					
		}
		else {

			tString=tString+parLine.charAt(tCursor);
			tCursor++;			
			tFlag=false;
				
			}
		}
		if (tFlag==false) {tStringArrayList.add(tString);}
		String[] tParsedStrings = new String[tStringArrayList.size()];
		return tStringArrayList.toArray(tParsedStrings);
	}
	
	

}

